package br.com.assemble.hermeserp.ejb.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

/**
 * Classe que representa um conjunto de dados advindo de uma recordset
 * @author Pacheco
 *
 */
public class ResultSetModel extends DefaultTableModel implements Serializable{

	private static final long serialVersionUID = 1L;
	// tipos de dados
	private List<TypeDef> typesDef = new Vector<TypeDef>();
	// dados -> aqui teremos um Vector<Vector<Object>>
	private ArrayList<Vector<Object>> records = new ArrayList<Vector<Object>>();
	
	/**
	 * Adicionando um tipo de dados (coluna)
	 * @param field
	 * @param type
	 */
	public void addTypeDef( final String field, final Class<?> type ){
		typesDef.add( new TypeDef( field, type ) );
	}

	/**
	 * Dados
	 * @param data
	 */
	public void addData( final Vector<Object> data ){
		records.add( data );
	}

	
	/**
	 * Inner class: tipo de dados (nome e type)
	 * @author Pacheco
	 *
	 */
	public class TypeDef implements Serializable{
		private static final long serialVersionUID = 1L;
		String fieldName;
		Class<?>  type;
		public TypeDef(String fieldName, Class<?> type) {
			super();
			this.fieldName = fieldName;
			this.type = type;
		}
		public String getFieldName() {
			return fieldName;
		}
		public void setFieldName(String fieldName) {
			this.fieldName = fieldName;
		}
		public Class<?> getType() {
			return type;
		}
		public void setType(Class<?> type) {
			this.type = type;
		}
	}

	public List<Vector<Object>> getRecords() {
		return records;
	}

	public List<TypeDef> getTypesDef() {
		return typesDef;
	}
	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return typesDef.size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int column) {
		return typesDef.get(column).fieldName;
	}
	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return typesDef.get(columnIndex).type;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return records == null ? 0 : records.size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int row, int column) {
		return records.get(row).get(column);
	}
}